package com.qf.leadnewsarticle.controller.v1.feign;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.leadnewsarticle.model.vos.ArticleVo;
import com.qf.leadnewsarticle.service.ApArticleService;
import com.qf.leadnewsarticle.service.RedisService;
import com.qf.leadnewsfeignapi.article.ArticleApi;
import com.qf.leadnewsmodel.consts.BehaviorConst;
import com.qf.leadnewsmodel.pojos.article.ApArticle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class ArticleFeignApi implements ArticleApi {

    @Autowired
    private ApArticleService apArticleService;

    @GetMapping("/api/feign/article/computeHot")
    public void computeHot() {
        //获取文章List （一般热点一周的热度，可以获取发布时间为当前时间到前七天的文章数据进行分析）
        Calendar calendar = Calendar.getInstance();
        //将发布时往前算7天
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        Date time = calendar.getTime();

        log.info("-----前7天的时间点： " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));

        LambdaQueryWrapper<ApArticle> qw = new LambdaQueryWrapper<>();
        qw.ge(ApArticle::getPublishTime,time);

//        List<ApArticle> list = apArticleService.list(qw);
        List<ApArticle> list = apArticleService.list();

        //计算每个文章的行为分数
        List<ArticleVo> articleVos = new ArrayList<>();
        for (ApArticle apArticle : list) {
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(apArticle,articleVo);
            //计算分数
            long score = computeScore(apArticle.getId());
            //将分数设置到vo对象中
            articleVo.setScore(score);
            articleVos.add(articleVo);
        }

        //根据行为分数对文章进行排序
        Collections.sort(articleVos);

        log.info("排序后的文章数据： " + articleVos);

        /*
            todo：查询所有频道
                从 articleVos 中获取指定频道的前30条数据
                分别调用cache将数据存入对应频道缓存
         */

        //取前30条文章记录存入的redis
        List<ArticleVo> articleVos1 = articleVos.stream().limit(30).collect(Collectors.toList());
        //缓存的是推荐页的数据
        redisService.cache("all",articleVos1);
    }

    @Autowired
    private RedisService redisService;

    /**
     * 根据用户的行为，计算该文章的分值
     * @param id
     * @return
     */
    private long computeScore(Long id) {
        String readKey = BehaviorConst.ARTICLE_READ_PREFIX + id;
        String likeKey = BehaviorConst.ARTICLE_LIKES_PREFIX + id;
        String collectionKey = BehaviorConst.ARTICLE_COLLECTION_PREFIX + id;

        //获取当前文章的阅读量，点赞量，收藏量
        long readCount = redisService.getBitmapCount(readKey);
        long likeCount = redisService.getBitmapCount(likeKey);
        long collectionCount = redisService.getBitmapCount(collectionKey);

        long score = 0;
        score += readCount*BehaviorConst.READ_WEIGHT;
        score += likeCount*BehaviorConst.LIKE_WEIGHT;
        score += collectionCount*BehaviorConst.COLLECTION_WEIGHT;

        log.info("=======文章id为：{}，行为分值为：{}",id,score);
        return score;
    }
}
